package com.ddwl.wallet.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Submit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.wallet.UserWithdrawSubmitBo;
import com.ddwl.schema.bo.wallet.WithdrawListQueryBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.wallet.UserWithdrawRecordVo;
import com.ddwl.wallet.service.WithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author Admin
 */
@Api(tags = "提现相关接口")
@RestController
@RequestMapping("Withdraw")
public class WithdrawController extends BaseController {

    @Resource
    private WithdrawService withdrawService;


    @Submit
    @ApiOperation(value = "提现申请提交")
    @PostMapping("/submit")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO})
    public RestVo submitWithdraw(@Valid @RequestBody UserWithdrawSubmitBo submitBo) {
        submitBo.setUserId(getUserId());
        submitBo.setProduct(getProduct());
        return RestVo.ok(withdrawService.submitWithdraw(submitBo).getId());
    }

    @PostMapping("/page")
    @ApiOperation("管理后台查询提现记录")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<UserWithdrawRecordVo>> queryWithdrawRecord(@RequestBody PageBo<WithdrawListQueryBo> bo) {
        if (bo.getParam() == null) {
            bo.setParam(new WithdrawListQueryBo());
        }
        return RestVo.ok(withdrawService.queryWithdrawRecord(bo));
    }

    @PostMapping("/export")
    @ApiOperation("管理后台导出提现记录")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public void export(@RequestBody WithdrawListQueryBo bo, HttpServletResponse response) throws Exception {
        if (bo == null) {
            bo = new WithdrawListQueryBo();
        }
        withdrawService.export(bo, response);
    }

    @PostMapping("/queryWithdrawRecordList")
    @ApiOperation("管理后台查询提现记录不分页")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<Integer> queryWithdrawRecordCount(@RequestBody WithdrawListQueryBo bo) {
        return RestVo.ok(withdrawService.queryWithdrawRecordList(bo));
    }

    @PostMapping("/userPage")
    @ApiOperation("APP查询当前登录用户提现记录")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO})
    public RestVo<PageVo<UserWithdrawRecordVo>> queryUserWithdrawRecord(@RequestBody PageBo<WithdrawListQueryBo> bo) {
        if (bo.getParam() == null) {
            bo.setParam(new WithdrawListQueryBo());
        }
        bo.getParam().setUserId(getUserId());
        return RestVo.ok(withdrawService.queryWithdrawRecord(bo));
    }

    @PostMapping("/{applyId}/paymentFail")
    @ApiOperation("转账失败")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo paymentFail(@PathVariable("applyId") String applyId, @RequestParam("msg") String msg) {
        withdrawService.paymentFail(applyId, msg);
        return RestVo.ok();
    }


    @Submit
    @PostMapping("/{applyId}/payment")
    @ApiOperation("转账成功")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo payment(@PathVariable("applyId") String applyId, @RequestParam("payFlowNo") String payFlowNo) {
        withdrawService.payment(applyId, payFlowNo);
        return RestVo.ok();
    }


    @ApiOperation("查询用户体现详情")
    @PostMapping("/user/flow/info")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO})
    public RestVo<UserWithdrawRecordVo> queryTxInfo(String id) {
        return RestVo.ok(withdrawService.getUserWithdrawRecord(id, getUserId()));
    }

}
